/*
 * buff_fifo.c
 *
 *  Created on: Sep 27, 2018
 *      Author: lidq
 */

#include <buff_fifo.hpp>

BuffFifo::BuffFifo()
{
	m_Buff = (char*)malloc(BUFF_FIFO_SIZE);
	memset(m_Buff, 0, sizeof(BUFF_FIFO_SIZE));
	m_Head = 0;
	m_Foot = 0;
}

BuffFifo::~BuffFifo(void)
{
	free(m_Buff);
}

void BuffFifo::SetBuff(char* value)
{
	m_Buff = value;
}

char* BuffFifo::GetBuff() const
{
	return m_Buff;
}

void BuffFifo::SetHead(int value)
{
	m_Head = value;
}

int BuffFifo::GetHead() const
{
	return m_Head;
}

void BuffFifo::SetFoot(int value)
{
	m_Foot = value;
}

int BuffFifo::GetFoot() const
{
	return m_Foot;
}

int BuffFifo::Append(char* pData, uint32_t len)
{
	if (pData == NULL)
	{
		LOG("[Err] protocol data == NULL\n");
		return -1;
	}

	if (len <= 0)
	{
		LOG("[Err] protocol len <=0\n");
		return -1;
	}

	for (uint32_t i = 0; i < len; i++)
	{
		m_Buff[m_Head] = pData[i];
		m_Head++;
		m_Head %= BUFF_FIFO_SIZE;
		if (m_Head == m_Foot)
		{
			m_Foot++;
			m_Foot %= BUFF_FIFO_SIZE;
		}
	}

	return 0;
}

int BuffFifo::Size(void)
{
	int len = m_Head - m_Foot;
	if (len < 0)
	{
		len += BUFF_FIFO_SIZE;
	}
	return len;
}
